package com.gzx.plugin.tjzy.modular.call.vos.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.gzx.plugin.tjzy.modular.call.vos.mapper.GzxVosAccessGatewayMapper;
import com.gzx.plugin.tjzy.modular.call.vos.resp.GzxVosAccessGatewayResp;
import com.gzx.plugin.tjzy.modular.call.vos.service.GzxVosAccessGatewayDbService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.util.CommonDateUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/5/16 16:47
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GzxVosAccessGatewayDbServiceImpl implements GzxVosAccessGatewayDbService {

    private final GzxVosAccessGatewayMapper vosAccessGatewayMapper;

    /**
     * 批量保存网关数据
     *
     * @param vosAccessGatewayDtoList
     */
    @Override
    @DS("tjzy-async")
    public void saveBatch(List<GzxVosAccessGatewayResp> vosAccessGatewayDtoList) {
        long startTime = System.currentTimeMillis();
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        String sql = vosAccessGatewayDtoList.stream()
                .map(s -> String.format("('%s', '%s', '%s', '%s', '%s','%s','%s')",
                        escapeSql(s.getName()),
                        escapeSql(String.valueOf(s.getCurrentCall())),
                        escapeSql(String.valueOf(s.getCapacity())),
                        escapeSql(String.valueOf(s.getAsr())),
                        escapeSql(String.valueOf(s.getAcd())),
                        escapeSql(JSON.toJSONString(s)),
                        escapeSql(currentTime)))
                .collect(Collectors.joining(", "));
        vosAccessGatewayMapper.insertBatch(sql.replace("'null'", "null"));
        log.info("保存VOS对接网关数据结束，共计{}条，耗时：{}", System.currentTimeMillis() - startTime, vosAccessGatewayDtoList.size());
    }
}
